/**
 * FileName: CollectionTests Author:   baoy Date:     2018/10/12 14:34 Description:
 */
package com.baoy.xxx.guava.collections;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import com.google.common.collect.Tables;
import com.google.common.primitives.Ints;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CollectionTests {


    public static void main(String[] arg) {
        //Lists
        List<Integer> intList = Ints.asList(4, 5, 6);
        List<String> theseElements = Lists.newArrayList("alpha", "beta", "gamma");
        //反转List
        List<String> thoseElements = Lists.reverse(theseElements);
        //指定大小分割
        List<List<String>> conList = Lists.partition(thoseElements, 3);
        //Sets
        Set<String> wordsWithPrimeLength = ImmutableSet
                .of("one", "two", "three", "six", "seven", "eight");
        Set<String> primes = ImmutableSet.of("two", "three", "five", "seven");
        //联合两个set
        Sets.union(wordsWithPrimeLength, primes);
        //求交集
        Sets.intersection(wordsWithPrimeLength, primes);

        //Maps
        Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
        Map<String, Integer> right = ImmutableMap.of("b", 2, "c", 4, "d", 4);
        //Maps.difference(Map, Map)用来比较两个Map以获取所有不同点。该方法返回MapDifference对象，把不同点的维恩图分解
        MapDifference<String, Integer> diff = Maps.difference(left, right);
        //找到键值对都相等的
        diff.entriesInCommon(); // {"b" => 2}
        //找到键相等值不想等的
        diff.entriesDiffering(); // {"c" => 3},{"c" => 4}
        //找到只存在于左边的
        diff.entriesOnlyOnLeft(); // {"a" => 1}
        //找到只存在于右边的
        diff.entriesOnlyOnRight(); // {"d" => 5}

        //Iterables工具类连接两个Iterable（集合）
        Iterable<Integer> concatenated = Iterables.concat(
                Ints.asList(1, 2, 3),
                Ints.asList(4, 5, 6)); // concatenated包括元素 1, 2, 3, 4, 5, 6

        //Tables
        Table<Integer, Integer, Integer> table = HashBasedTable.create();
        //把Table<C, R, V>转置成Table<R, C, V> 也就是行列互换
        Tables.transpose(table);
    }
}
